/*
 * @(#)MessageRetriever.java 1.25 04/04/27
 * 
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. SUN
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.sun.tools.doclets.internal.toolkit.util;

import com.sun.javadoc.*;
import com.sun.tools.doclets.internal.toolkit.Configuration;
import java.util.*;
import java.text.MessageFormat;

/**
 * Retrieve and format messages stored in a resource.
 * 
 * This code is not part of an API. It is implementation that is subject to
 * change. Do not use it as an API
 * 
 * @since JDK1.2
 * @author Atul M Dambalkar
 * @author Robert Field
 */
public class MessageRetriever {
	/**
	 * The global configuration information for this run.
	 */
	private final Configuration configuration;
	
	/**
	 * The location from which to lazily fetch the resource..
	 */
	private final String resourcelocation;
	
	/**
	 * The lazily fetched resource..
	 */
	private ResourceBundle messageRB;
	
	/**
	 * Initilize the ResourceBundle with the given resource.
	 * 
	 * @param rb the esource bundle to read.
	 */
	public MessageRetriever(ResourceBundle rb) {
		this.configuration = null;
		this.messageRB = rb;
		this.resourcelocation = null;
	}
	
	/**
	 * Initilize the ResourceBundle with the given resource.
	 * 
	 * @param configuration the configuration
	 * @param resourcelocation Resource.
	 */
	public MessageRetriever(Configuration configuration, String resourcelocation) {
		this.configuration = configuration;
		this.resourcelocation = resourcelocation;
	}
	
	/**
	 * get and format message string from resource
	 * 
	 * @param key selects message from resource
	 */
	public String getText(String key) {
		return getText(key, (String) null);
	}
	
	/**
	 * Get and format message string from resource
	 * 
	 * @param key selects message from resource
	 * @param a1 Argument, to be repalced in the message.
	 */
	public String getText(String key, String a1) {
		return getText(key, a1, null);
	}
	
	/**
	 * Get and format message string from resource
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public String getText(String key, String a1, String a2) {
		return getText(key, a1, a2, null);
	}
	
	/**
	 * Get and format message string from resource
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 * @throws MissingResourceException when the key does not exist in the
	 *         properties file.
	 */
	public String getText(String key, String a1, String a2, String a3)
			throws MissingResourceException {
		if (messageRB == null) {
			try {
				messageRB = ResourceBundle.getBundle(resourcelocation,
						Locale.getDefault());
			}
			catch (MissingResourceException e) {
				throw new Error("Fatal: Resource (" + resourcelocation
						+ ") for javadoc doclets is missing.");
			}
		}
		String message = messageRB.getString(key);
		return MessageFormat.format(message, a1, a2, a3);
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param pos the position of the source
	 * @param msg message to print
	 */
	private void printError(SourcePosition pos, String msg) {
		configuration.root.printError(pos, msg);
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param msg message to print
	 */
	private void printError(String msg) {
		configuration.root.printError(msg);
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param pos the position of the source
	 * @param msg message to print
	 */
	private void printWarning(SourcePosition pos, String msg) {
		configuration.root.printWarning(pos, msg);
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param msg message to print
	 */
	private void printWarning(String msg) {
		configuration.root.printWarning(msg);
	}
	
	/**
	 * Print a message.
	 * 
	 * @param pos the position of the source
	 * @param msg message to print
	 */
	private void printNotice(SourcePosition pos, String msg) {
		configuration.root.printNotice(pos, msg);
	}
	
	/**
	 * Print a message.
	 * 
	 * @param msg message to print
	 */
	private void printNotice(String msg) {
		configuration.root.printNotice(msg);
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 */
	public void error(SourcePosition pos, String key) {
		printError(pos, getText(key));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param key selects message from resource
	 */
	public void error(String key) {
		printError(getText(key));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 */
	public void error(SourcePosition pos, String key, String a1) {
		printError(pos, getText(key, a1));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 */
	public void error(String key, String a1) {
		printError(getText(key, a1));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public void error(SourcePosition pos, String key, String a1, String a2) {
		printError(pos, getText(key, a1, a2));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public void error(String key, String a1, String a2) {
		printError(getText(key, a1, a2));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 */
	public void error(SourcePosition pos, String key, String a1, String a2,
			String a3) {
		printError(pos, getText(key, a1, a2, a3));
	}
	
	/**
	 * Print error message, increment error count.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 */
	public void error(String key, String a1, String a2, String a3) {
		printError(getText(key, a1, a2, a3));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 */
	public void warning(SourcePosition pos, String key) {
		printWarning(pos, getText(key));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param key selects message from resource
	 */
	public void warning(String key) {
		printWarning(getText(key));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 */
	public void warning(SourcePosition pos, String key, String a1) {
		printWarning(pos, getText(key, a1));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 */
	public void warning(String key, String a1) {
		printWarning(getText(key, a1));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public void warning(SourcePosition pos, String key, String a1, String a2) {
		printWarning(pos, getText(key, a1, a2));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public void warning(String key, String a1, String a2) {
		printWarning(getText(key, a1, a2));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 */
	public void warning(SourcePosition pos, String key, String a1, String a2,
			String a3) {
		printWarning(pos, getText(key, a1, a2, a3));
	}
	
	/**
	 * Print warning message, increment warning count.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 */
	public void warning(String key, String a1, String a2, String a3) {
		printWarning(getText(key, a1, a2, a3));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 */
	public void notice(SourcePosition pos, String key) {
		printNotice(pos, getText(key));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param key selects message from resource
	 */
	public void notice(String key) {
		printNotice(getText(key));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 */
	public void notice(SourcePosition pos, String key, String a1) {
		printNotice(pos, getText(key, a1));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 */
	public void notice(String key, String a1) {
		printNotice(getText(key, a1));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public void notice(SourcePosition pos, String key, String a1, String a2) {
		printNotice(pos, getText(key, a1, a2));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 */
	public void notice(String key, String a1, String a2) {
		printNotice(getText(key, a1, a2));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param pos the position of the source
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 */
	public void notice(SourcePosition pos, String key, String a1, String a2,
			String a3) {
		printNotice(pos, getText(key, a1, a2, a3));
	}
	
	/**
	 * Print a message.
	 * 
	 * @param key selects message from resource
	 * @param a1 first argument to be replaced in the message.
	 * @param a2 second argument to be replaced in the message.
	 * @param a3 third argument to be replaced in the message.
	 */
	public void notice(String key, String a1, String a2, String a3) {
		printNotice(getText(key, a1, a2, a3));
	}
}
